package com.east.controller.system;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.east.bean.User;
import com.east.service.system.UserService;
import com.east.utils.PageJson;
import com.east.utils.Pagenation;
import com.east.utils.Result;
import com.east.utils.ResultUtil;
import com.github.pagehelper.Page;

@RestController
@RequestMapping("user")
public class UserController  {
	@Autowired
	UserService userService;
	
	/**根据用户
	 * 获取角色列表
	 * @param account
	 * @param pwd
	 * @return
	 */
	@GetMapping("/all")
	@RequiresPermissions("user:all")
	public Result finbyUser(Pagenation pagenation,String key) {
		System.out.println(JSON.toJSON(pagenation));
		Page<User> findUserAll = userService.findUserAll(pagenation, key);
		Object pageJson = new PageJson<User>().pageJson(findUserAll);
		return ResultUtil.success(pageJson);
	}
	@GetMapping("/findOne")
	@RequiresPermissions("user:findOne")
	public Result findOne(User user) {
		User findByUserId = userService.findByUserId(user);
		return ResultUtil.success(findByUserId);
	}
	@GetMapping("/account")
	@RequiresPermissions("user:account")
	public Result findAccount(User user) {
		boolean findByUserId = userService.findByAccoount(user);
		return ResultUtil.success(findByUserId);
	}
	@PostMapping("/update")
	@RequiresPermissions("user:update")
	public Result update(User user,String userRoles) {
		String[] split=null;
		if(StringUtils.isNotBlank(userRoles)) {
			 split = userRoles.split(",");
		}
		
		int updateUser = userService.updateUser(user, split);
		if(updateUser>0) {
			return ResultUtil.success("修改成功");
		}else {
			return ResultUtil.success("修改失败");
		}
		
	}
	/**
	 * 修改密码
	 * @param user
	 * @param userRoles
	 * @return
	 */
	@PostMapping("/pwd")
	public Result updatePwd(User user) {
		int updateUser = userService.updateUser(user);
		if(updateUser>0) {
			return ResultUtil.success("修改成功");
		}else {
			return ResultUtil.success("修改失败");
		}
		
	}
	
	
	
}
